package com.example.yls.demoa;

import android.os.PersistableBundle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdate;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements AMap.OnMapLongClickListener,PoiSearch.OnPoiSearchListener{
    private AMap aMap;
    private MapView mapView;
    private EditText edtKey;
    private Button btnSearch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mapView = findViewById(R.id.mapView);
        aMap =mapView.getMap();
        mapView.onCreate(savedInstanceState);

        CameraUpdate mCameraUpdate = CameraUpdateFactory.newCameraPosition
                (new CameraPosition(new LatLng(23.18321,113.353111),
                             15,30,0));
        aMap.moveCamera(mCameraUpdate);
        aMap.setOnMapLongClickListener(this);

        initSearch();
    }

    private void initSearch() {
        btnSearch = findViewById(R.id.btn_search);
        edtKey = findViewById(R.id.edt_key);

        btnSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String key = edtKey.getText().toString();
                PoiSearch.Query query = new PoiSearch.Query(key,"","020");
                query.setPageSize(10);//设置每页最多返回多少条poiitem
                query.setPageNum(0);

                PoiSearch poiSearch = new PoiSearch(MainActivity.this,query);
                poiSearch.setBound(new PoiSearch.SearchBound
                        (new LatLonPoint(23.18321,113.353111),
                                2000));

                poiSearch.setOnPoiSearchListener(MainActivity.this);
                poiSearch.searchPOIAsyn();
            }
        });
    }

    @Override
    public void onMapLongClick(LatLng latLng) {
        Log.e("MainActivity","latitude"+latLng.latitude+"logitude="+latLng.longitude);
        Marker mar = aMap.addMarker(new MarkerOptions().position(latLng)
                .title("广东交通职业技术学院").snippet("欢迎您"));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }


    @Override
    public void onPoiSearched(PoiResult poiResult, int i) {
        Log.e("MainActivity","onPoiSearched");
        if (poiResult == null){
            return;
        }

        ArrayList<PoiItem> poiItemList = poiResult.getPois();
        for(PoiItem poiItem : poiItemList){
            Log.e("MainActivity",poiItem.getTitle());
            double latitude = poiItem.getLatLonPoint().getLatitude();
            double longitude = poiItem.getLatLonPoint().getLongitude();

            LatLng latLng = new LatLng(latitude,longitude);
            aMap.addMarker(new MarkerOptions().position(latLng)
                    .title(poiItem.getTitle())
                    .snippet(poiItem.getTel()));
        }

    }

    @Override
    public void onPoiItemSearched(PoiItem poiItem, int i) {
        Log.e("MainActivity","onPoiItemSearched");
    }

    //截图快捷键CTRL+SHIFT+Q
}
